package com.group9.handinhand.dao.model;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang.ArrayUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.group9.handinhand.service.so.CommunityMemberSO;

@Entity
@Table(name = "COMMUNITY_MEMBER", uniqueConstraints = @UniqueConstraint(columnNames = {"USER_ID", "COMMUNITY_ID" }))
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class CommunityMember extends AbstractBaseModel {

	private static final long serialVersionUID = 1L;
	
	@ManyToOne
    @JoinColumn(name = "COMMUNITY_ID", nullable = false)
	private Community community;
	
	@ManyToOne
    @JoinColumn(name = "USER_ID", nullable = false)
	private User user;
	
	public CommunityMemberSO convertToServiceObject(String... ignoreProperties) {
		CommunityMemberSO so = (CommunityMemberSO) convert(new CommunityMemberSO(), 
			(String[]) ArrayUtils.addAll(ignoreProperties, new String[]{"community","user"}));
		so.setCommunity(community.convertToServiceObject(new String[]{"moderators", "posts", "events","requests","offers","members"}));
		so.setUser(user.convertToServiceObject(new String[]{"socialUsers"}));
		return so;
	}

	public Community getCommunity() {
		return community;
	}

	public void setCommunity(Community community) {
		this.community = community;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public String toString() {
		return "CommunityMember [community=" + community + ", user=" + user
				+ "]";
	}

}
